<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleInit"
          >初始化缓存</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="collectPointList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="点位类型" align="center" prop="pointType" :formatter="pointTypeFormat"/>
      <el-table-column label="MN号" align="center" prop="mn" />
      <el-table-column label="通信卡号" align="center" prop="careMn" />
      <el-table-column label="责任人员" align="center" prop="dutyUser" />
      <el-table-column label="所在地" align="left" prop="address" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="290">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDetail(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleParam(scope.row)"
          >参数</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleVideo(scope.row)"
          >视频</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleMeter(scope.row)"
          >仪表</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button v-if="scope.row.ispmt === 'Y'"
            size="mini"
            type="text"
            @click="handleLonLat(scope.row)"
          >位置</el-button>
          <el-button v-if="scope.row.isgy === 'Y'"
            size="mini"
            type="text"
            @click="handleGy(scope.row)"
          >工艺</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采集信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="图片">
              <uploadImage v-model="form.pic"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="MN号" prop="mn">
              <el-input v-model="form.mn" placeholder="请输入MN号" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="通信卡号" prop="careMn">
              <el-input v-model="form.careMn" placeholder="请输入通信卡号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="点位类型" prop="pointType">
              <el-select v-model="form.pointType" placeholder="请选择点位类型">
                <el-option
                  v-for="dict in pointTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="责任人员" prop="dutyUser">
              <el-input v-model="form.dutyUser" placeholder="请输入责任人员" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="日到报数" prop="sort">
              <el-input-number v-model="form.dayNum" placeholder="请输入日到报数" :precision="0" :min="1" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="sort">
              <el-input-number v-model="form.sort" placeholder="请输入排序" :precision="0" :min="1" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="归属部门" prop="deptId">
              <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="归属项目" prop="protreeId">
              <treeselect v-model="form.protreeId" :options="protreeOptions" :show-count="true" placeholder="请选择归属项目" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目类型" prop="proType">
              <el-select v-model="form.proType" placeholder="请选择项目类型" >
                <el-option
                  v-for="dict in proTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据存储" prop="isUse">
              <el-select v-model="form.isDs" placeholder="请选择是否数据存储">
                <el-option
                  v-for="dict in sysIsOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="所在地" prop="address">
              <el-input v-model="form.address" placeholder="请输入所在地" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" :rows="4" v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-divider content-position="left">通讯协议配置</el-divider>
          </el-col>

          <el-col :span="12">
            <el-form-item label="通讯模式" prop="txmode">
              <el-select v-model="form.txmode" placeholder="请选择通讯模式">
                <el-option
                  v-for="dict in txmodeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="协议类型" prop="agreementId">
              <el-select v-model="form.agreementId" placeholder="请选择协议类型">
                <el-option
                  v-for="agreementInfo in agreementInfoList"
                  :key="agreementInfo.id"
                  :label="agreementInfo.agreementName"
                  :value="agreementInfo.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="通讯标识" prop="txcdkey">
              <el-input v-model="form.txcdkey" placeholder="请输入通讯标识" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="通讯密码" prop="txpwd">
              <el-input v-model="form.txpwd" placeholder="4至6位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="通讯端口" prop="txprot">
              <el-input-number v-model="form.txprot" placeholder="请输入通讯端口" :precision="0" :min="1" :max="65535" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="通讯类别" prop="commType">
              <el-input v-model="form.commType" placeholder="请输入通讯类别" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机架号" prop="commRack">
              <el-input v-model="form.commRack" placeholder="请输入PLC机架号" oninput="value=value.replace(/^\.+|[^\d-]/g,'')"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="PLC槽号" prop="commSlot">
              <el-input v-model="form.commSlot" placeholder="请输入PLC槽号"  oninput="value=value.replace(/^\.+|[^\d-]/g,'')"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请求间隔" prop="reqInterval">
              <el-input v-model="form.reqInterval" placeholder="请输入请求间隔"  oninput="value=value.replace(/^\.+|[^\d-]/g,'')"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="通讯地址" prop="commAddr">
              <el-input v-model="form.commAddr" placeholder="请输入通讯地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请求命令" prop="reqCmd">
              <el-input v-model="form.reqCmd" placeholder="请输入请求命令"  oninput="value=value.replace(/^\.+|[^\d-]/g,'')"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请求编码" prop="reqCode">
              <el-input v-model="form.reqCode" placeholder="请输入请求编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否启用" prop="isUse">
              <el-select v-model="form.isUse" placeholder="请选择是否启用">
                <el-option
                  v-for="dict in sysIsOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报文上传" prop="upFrame">
              <el-select v-model="form.upFrame" placeholder="请选择是否上传报文">
                <el-option
                  v-for="dict in sysIsOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-divider content-position="left">工艺配置</el-divider>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工艺显示" prop="isgy">
              <el-select v-model="form.isgy" placeholder="请选择是否工艺显示">
                <el-option
                  v-for="dict in sysIsOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地图显示" prop="ispmt">
              <el-select v-model="form.ispmt" placeholder="请选择是否地图显示">
                <el-option
                  v-for="dict in sysIsOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>


          <el-col :span="24">
            <el-form-item label="工艺图片">
              <uploadImage v-model="form.gypic"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <span>注：工艺图最大分辨率为1445*800，超出恐造成变形！</span>
          </el-col>

        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看对话框 -->
    <el-dialog title="站点详细信息" :visible.sync="detailOpen" width="640px" append-to-body >
      <div>
        <detailConf :pointId="pointId"></detailConf>
      </div>
    </el-dialog>
    <!-- 编码配置对话框 -->
    <el-dialog title="参数配置" :visible.sync="paramOpen" width="740px" append-to-body >
      <div>
        <paramConf :pointId="pointId"></paramConf>
      </div>
    </el-dialog>
    <!-- 仪表配置对话框 -->
    <el-dialog title="仪表配置" :visible.sync="meterOpen" width="740px" append-to-body >
      <div>
        <meterConf :pointId="pointId"></meterConf>
      </div>
    </el-dialog>
    <!-- 视频配置对话框 -->
    <el-dialog title="视频配置" :visible.sync="videoOpen" width="740px" append-to-body >
      <div>
        <videoConf :pointId="pointId"></videoConf>
      </div>
    </el-dialog>
    <!-- 位置配置对话框 -->
    <el-dialog title="位置配置" :visible.sync="lonLatOpen" width="1240px" append-to-body >
      <siteLonLat :collectPoint="form" />
    </el-dialog>
    <!-- 工艺配置对话框 -->
    <el-dialog title="工艺配置" :visible.sync="gyOpen" v-if="gyOpen" width="1240px" append-to-body >
      <gy-lon-lat :collectPoint="form"></gy-lon-lat>
    </el-dialog>

  </div>
</template>

<script>
import { listCollectPoint, getCollectPoint, delCollectPoint, addCollectPoint, updateCollectPoint, exportCollectPoint,initCacheData } from "@/api/basic/collectPoint";
import UploadImage from '@/components/UploadImage';
import detailConf from "./detailConf";
import paramConf from "./paramConf";
import meterConf from "./meterConf";
import videoConf from "./videoConf";
import { listAgreementInfo } from "@/api/basic/agreementInfo";
import { treeselect } from "@/api/system/dept";
import { protreeTreeSelect } from "@/api/basic/protree";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import siteLonLat from "./siteLonLat";
import GyLonLat from "./gyLonLat";

export default {
  name: "CollectPoint",
  components: {
    GyLonLat,
    UploadImage,paramConf,meterConf,detailConf,Treeselect,siteLonLat,videoConf
  },
  data() {
    return {
      // 部门树选项
      deptOptions: undefined,
      protreeOptions: undefined,
      //  是否
      sysIsOptions: [{dictLabel:'否',dictValue:'N'},{dictLabel:'是',dictValue:'Y'}],
      // 字典
      pointTypeOptions: [],
      proTypeOptions:[],
      txmodeOptions:[{dictLabel:'客户端',dictValue:'Client'},{dictLabel:'服务端',dictValue:'Server'},{dictLabel:'串口',dictValue:'SerialPort'},{dictLabel:'未知',dictValue:'UnKnown'}],
      // cjmodeOptions:[{dictLabel:'通讯中间件',dictValue:'txa'},{dictLabel:'自来水远传',dictValue:'txb'}],
      pointId: null,
      ak:'PBN22I5Vj9bmNHV47jqkKwiezrwlU3jF',
      mapOpen: false,
      cityName: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采集信息管理表格数据
      collectPointList: [],
      agreementInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示参数弹窗
      detailOpen:false,
      paramOpen: false,
      meterOpen: false,
      videoOpen: false,
      lonLatOpen:false,
      gyOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        mn: [
          { required: true, message: "mn不能为空", trigger: "blur" }
        ],
        txmode: [
          { required: true, message: "通讯模式不能为空", trigger: "blur" }
        ],
        agreementId: [
          { required: true, message: "协议类型不能为空", trigger: "blur" }
        ],
        pointType: [
          { required: true, message: "站点类型不能为空", trigger: "blur" }
        ],
        careMn: [
          { required: true, message: "通讯卡号不能为空", trigger: "blur" }
        ],
        dutyUser: [
          { required: true, message: "责任人员不能为空", trigger: "blur" }
        ],
        txpwd: [
          { required: true, message: "通讯密码不能为空", trigger: "blur" }
        ],
        txprot: [
          { required: true, message: "通讯端口不能为空", trigger: "blur" }
        ],
        proType: [
          { required: true, message: "项目类型不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "归属部门不能为空", trigger: "change" }
        ],
        // cjmode: [
        //   { required: true, message: "采集方式不能为空", trigger: "change" }
        // ],
        protreeId: [
          { required: true, message: "归属项目不能为空", trigger: "change" }
        ],

      }
    };
  },
  created() {
    this.getTreeselect();
    this.getProtreeTreeselect();
    this.getAgreementInfoList();
    this.getDicts("basic_point_type").then(response => {
      this.pointTypeOptions = response.data;
    });
    this.getDicts("basic_pro_type").then(response => {
      this.proTypeOptions = response.data;
    });
    this.getList();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    getProtreeTreeselect() {
      protreeTreeSelect().then(response => {
        this.protreeOptions = response.data;
      });
    },
    /** 查询协议信息管理列表 */
    getAgreementInfoList() {
      let obj = {
        pageNum: 1,
        pageSize: 1000,
      }
      listAgreementInfo(obj).then(response => {
        this.agreementInfoList = response.rows;
      });
    },
    //字典翻译
    pointTypeFormat(row, column) {
      return this.selectDictLabel(this.pointTypeOptions, row.pointType);
    },
    mapQuery() {
      if(this.cityName === ""){

      }else{
        this.map.centerAndZoom(this.cityName,11);      // 用城市名设置地图中心点
      }
    },
    showMap(){
      this.mapOpen = true;
      this.$nextTick(() => {
        const _this = this
        MP(_this.ak).then(BMap => {
          _this.initMap();
        })
      });
    },
    /** 查询采集信息管理列表 */
    getList() {
      this.loading = true;
      listCollectPoint(this.queryParams).then(response => {
        this.collectPointList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        pointType: null,
        careMn: null,
        dutyUser: null,
        devInfo: null,
        factoryInfo: null,
        address: null,
        longitude: null,
        latitude: null,
        pic: null,
        gypic: null,
        remark: null,
        sort: null,
        dayNum: null,
        mn: null,
        txmode: null,
        agreementId: null,
        txcdkey: null,
        txpwd: null,
        txprot: null,
        proType:null,
        // cjmode:null,
        protreeId:null,
        rellongitude: undefined,
        rellatitude: undefined,
        commType: null,
        commRack: null,
        commSlot: null,
        reqInterval: null,
        commAddr: null,
        reqCmd: null,
        reqCode: null,
        isUse: 'Y',
        upFrame: 'N',
        isDs:'N',
        isgy:'N',
        ispmt:'Y',
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采集信息管理";
    },
    /** 位置配置 */
    handleLonLat(row) {
      this.reset();
      const id = row.id || this.ids
      getCollectPoint(id).then(response => {
        this.form = response.data;
        this.lonLatOpen = true;
      });
    },
    /** 工艺配置 */
    handleGy(row) {
      this.reset();
      const id = row.id || this.ids
      getCollectPoint(id).then(response => {
        this.form = response.data;
        this.gyOpen = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.getTreeselect();
      getCollectPoint(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改采集信息管理";
      });
    },
    handleDetail(row) {
      this.pointId = row.id;
      this.detailOpen = true;
    },
    /** 参数配置 */
    handleParam(row) {
      this.pointId = row.id;
      this.paramOpen = true;
    },
    /** 仪表配置 */
    handleMeter(row) {
      this.pointId = row.id;
      this.meterOpen = true;
    },
    handleVideo(row) {
      this.pointId = row.id;
      this.videoOpen = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCollectPoint(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCollectPoint(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;

      this.$confirm('是否确认删除监测站点为【' + row.name + '】的信息，以及历史存储表的数据?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCollectPoint(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有采集信息管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCollectPoint(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    //初始化缓存
    handleInit(){
      initCacheData().then(response => {
        this.msgSuccess("初始化成功");
      });
    },
  }
};
</script>
<style>
  /**<!-- 隐藏地图logo -->**/
  /*.anchorBL{display:none}*/
</style>
